package com.pactera.controller;

import com.pactera.util.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Pactera on 2017/1/9.
 */
@Controller
@RequestMapping("/email")
public class SenEmailController {

    /**
     * 跳转到发送邮件页面
     * @return
     */
    @RequestMapping("/goSendEmail")
    public ModelAndView senMail(String email){
        ModelAndView mv = new ModelAndView();
        if(!StringUtils.isEmpty(email)){
            email = email.trim();
            mv.addObject("email",email);
        }
        mv.setViewName("sendEmail");
        return mv;
    }
    /**
     * 发送邮件
     * @return
     */
    @RequestMapping(value="/sendEmail",produces="application/json;charset=UTF-8")
    @ResponseBody
    public Object senMail(HttpServletRequest request){
        Map<String,String> map = new HashMap<String, String>();
        String msg = "ok";
        try {
            String toMail = request.getParameter("EMAIL");
            String tilte = request.getParameter("TITLE");
            String content= request.getParameter("CONTENT");
            JavaMailSender.sendMail(toMail,tilte,content);
        }catch (Exception e){
            msg = "failure";
        }
        map.put("result",msg);
        return map;
    }
}
